package com.songshuai.webapi.common.upload;

import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaseUploadFile {

    Logger logger = LoggerFactory.getLogger(BaseUploadFile.class);

    private SnowFlakeUtil snowFlakeUtil;

    private String fileBasePath;

    private OssUploadUtil uploadUtil;

    public BaseUploadFile(SnowFlakeUtil snowFlakeUtil, String fileBasePath, OssUploadUtil uploadUtil) {
        this.snowFlakeUtil = snowFlakeUtil;
        this.fileBasePath = fileBasePath;
        this.uploadUtil = uploadUtil;
    }

    //生成新的文件名上传
    public Map<String,Object> uploadFile(MultipartFile file, String type)throws Exception{
        String contentType = file.getContentType();   //文件类型
        String fileName = file.getOriginalFilename();  //文件名字
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String fname = snowFlakeUtil.nextId() + suffix;  //生成新的文件名
        String uploadPath = "";
        if("image".equals(type)) {
            String[] content=contentType.split("/");
            if (content.length>0){
                if("image".equalsIgnoreCase(content[0])){
                    //文件存放路径
                    String filePath = fileBasePath+ File.separator+type;
                    uploadPath=getFileUrl(file,filePath,fname);
                }else{
                    logger.info("判断为非图片文件");
                    return getResult(uploadPath,contentType,fname,fileName,1,"请上传图片文件");
                }
            }else {
                //非常规文件
                logger.info("非常规文件");
                return getResult(uploadPath,contentType,fname,fileName,1,"无法识别图片类型");
            }
        }else{

            //todo:非图片-视频上传
            String filePath = fileBasePath+ File.separator+type;
            uploadPath=getFileUrl(file,filePath,fname);

            return getResultVideo(uploadPath,contentType,fname,fileName,0,"上传成功");
           /* new Thread(()->{

            }).start();*/
        }
        return getResult(uploadPath,contentType,fname,fileName,0,"上传成功");
    }

    //使用原生的文件名上传，如果服务器存在相同文件会覆盖原文件
    public Map<String,Object> uploadFile(MultipartFile file,String type,Boolean useOriginalFilename)throws Exception{
        if(useOriginalFilename){
            String contentType = file.getContentType();   //文件类型
            String fileName = file.getOriginalFilename();  //文件名字
            String uploadPath = "";
            if("image".equals(type)) {
                //文件存放路径
                String filePath = fileBasePath+ File.separator+type;
                uploadPath=getFileUrl(file,filePath,fileName);

            }else{
                //todo:非图片
           /* new Thread(()->{

            }).start();*/
            }
            return getResult(uploadPath,contentType,fileName,fileName,0,"上传成功");
        }else{
            return  this.uploadFile(file,type);
        }
    }


    //获取图片URL
    String getFileUrl (MultipartFile file,String Path,String name) throws IOException {
        boolean upload = uploadUtil.upload(file.getInputStream(), Path + name);
        if (upload) {
            String   uploadPath = uploadUtil.getOSSUrl(Path + name, 87600);//十年
            return uploadPath;
        }
        return "";
    }


    //组装返回参数
    Map<String,Object> getResultVideo(String uploadPath,String contentType,String fname,String fileName,Integer code,String msg){
        Map<String,Object> result=new HashMap<>();
        result.put("uploadPath", uploadPath);
        result.put("videoType", contentType);
        result.put("videoName", fname);
        result.put("originalName", fileName);
        result.put("code",code);
        result.put("msg",msg);
        return result;
    }

    //组装返回参数
    Map<String,Object> getResult(String uploadPath,String contentType,String fname,String fileName,Integer code,String msg){
        Map<String,Object> result=new HashMap<>();
        result.put("uploadPath", uploadPath);
        result.put("pictureType", contentType);
        result.put("pictureName", fname);
        result.put("originalName", fileName);
        result.put("code",code);
        result.put("msg",msg);
        return result;
    }
}
